import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_massage/pages/message/message_bean.dart';
import 'package:flutter_massage/pages/model/bean.dart';
import 'package:flutter_massage/pages/my/address/address_bean.dart';
import 'package:flutter_massage/pages/technician/place_order/order_bean.dart';
import 'package:flutter_massage/sqlite/db_interface.dart';

class DbManager {
  DbManager._init();

  static final DbManager _instance = DbManager._init();

  factory DbManager() => _instance;

  DbInterface? _dbImplements;

  ///初始化
  void init({required DbInterface dbImplements}) => _dbImplements = dbImplements;

  ///打开数据库
  static Future openDb() async => await _instance._dbImplements!.openDb();

  ///关闭数据库
  static Future closeDb() async => await _instance._dbImplements!.closeDb();

  ///新增地址
  static Future addAddress(AddressBean addressBean) async => await _instance._dbImplements!.addAddress(addressBean);

  ///编辑地址
  static Future editAddress(AddressBean addressBean) async => await _instance._dbImplements!.editAddress(addressBean);

  ///删除地址
  static Future deleteAddress(String addressId) async => await _instance._dbImplements!.deleteAddress(addressId);

  ///地址列表
  static Future<List<AddressBean>> getAddressList() async => await _instance._dbImplements!.getAddressList();

  ///插入消息
  static Future insertMessage({required MessageBean messageBean}) async =>
      await _instance._dbImplements!.insertMessage(messageBean: messageBean);

  ///获取消息列表
  static Future<List<MessageBean>> getMessageList({required String chatId}) async =>
      await _instance._dbImplements!.getMessageList(chatId: chatId);

  ///获取会话列表
  static Future<List<ChatBean>> getChatList({required String userId}) async =>
      await _instance._dbImplements!.getChatList(userId: userId);

  ///插入、更新会话
  static Future updateChat({required ChatBean bean}) async => await _instance._dbImplements!.updateChat(bean: bean);

  ///更新会话的最后一条消息内容
  static Future updateChatLastMessage({required String chatId, required String lastMessage}) async =>
      await _instance._dbImplements!.updateChatLastMessage(chatId: chatId, lastMessage: lastMessage);

  ///存订单
  static Future saveOrder({
    required String orderId,
    required String time,
    required String desc,
    required AddressBean addressBean,
    required TechnicianBean technicianBean,
    required ProjectBean projectBean,
  }) async =>
      await _instance._dbImplements!.saveOrder(
        orderId: orderId,
        time: time,
        desc: desc,
        addressBean: addressBean,
        technicianBean: technicianBean,
        projectBean: projectBean,
      );

  ///获取订单列表
  static void getOrderList({required ValueChanged<List<OrderBean>> complete}) =>
      _instance._dbImplements!.getOrderList(complete: complete);
}
